package com.example.healkep;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.icu.text.IDNA;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.example.healkep.echarts.EchartOptionUtil;
import com.example.healkep.echarts.EchartView;
import com.example.healkep.echarts.WchartView;
import com.example.healkep.utils.GetWeek;
import com.example.healkep.utils.InformationUtils;

import org.json.JSONObject;

import java.util.Objects;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class InformationActivity extends AppCompatActivity {

    private final String username = LoginActivity.getUser();
    private final String sessionid = LoginActivity.getSessionID();
    private double[] weightsdata =new double[42];

    @SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        if (getSupportActionBar() != null){
            getSupportActionBar().hide();
        }
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_information);
        Button back = findViewById ( R.id.back );
        back.setOnClickListener ( new View.OnClickListener () {
            @Override
            public void onClick(View view) {
                finish ();
            }
        } );

        TextView name_tv = findViewById(R.id.name_tv);
        TextView tbirth = findViewById(R.id.tbirth);
        TextView theight1 = findViewById(R.id.theight1);
        TextView tweight1 = findViewById(R.id.tweight1);
        TextView tweight2 = findViewById(R.id.tweight2);
        TextView tweek = findViewById(R.id.tweek);
        TextView tnum = findViewById(R.id.tnum);
        TextView tage = findViewById(R.id.tage);
        TextView tenergy = findViewById(R.id.tenergy);
        TextView taveenergy = findViewById(R.id.taveenergy);
        TextView tenweight = findViewById(R.id.tenweight);
        TextView tlenweight = findViewById(R.id.tlenweight);
        TextView ffm = findViewById(R.id.tv_ffm);
        TextView protein = findViewById(R.id.tv_protein);
        TextView muscle = findViewById(R.id.tv_muscle);
        TextView ucre = findViewById(R.id.tv_ucre);
        Button getinform = findViewById(R.id.getinform);
        Button change = findViewById(R.id.changeinform);

        getinform.setOnClickListener(v -> {
            String json1 = "{\n" +
                    "    \"username\":\"" + username + "\"\n" +
                    "}";
            new Thread(() -> {
                try {
                    OkHttpClient clientget = new OkHttpClient();
                    Request request = new Request.Builder()
                            .url(LoginActivity.url+"/information/get")
                            .addHeader("Cookie", sessionid)
                            .post(RequestBody.create(MediaType.parse("application/json"), json1))
                            .build();
                    Response response = clientget.newCall(request).execute();
                    String resultget = Objects.requireNonNull(response.body()).string();
                    JSONObject jsonObject = new JSONObject(resultget);
                    try {
                        if (jsonObject.get("username").equals("!@#")) {
                            runOnUiThread(() -> Toast.makeText(InformationActivity.this, "登录超时,请重新登录", Toast.LENGTH_SHORT).show());
                            Intent intent = new Intent();
                            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                            intent.setClass(InformationActivity.this, LoginActivity.class);
                            startActivity(intent);
                        } else {
                            name_tv.setText(jsonObject.getString("clientName"));
                            tbirth.setText(jsonObject.getString("birth"));
                            theight1.setText(jsonObject.getString("height1"));
                            tweight1.setText(jsonObject.getString("weight1"));
                            String date=jsonObject.getString("date");
                            long week = GetWeek.week(date);
                            tweek.setText(""+week);
                            tnum.setText(jsonObject.getString("num"));
                            tage.setText(jsonObject.getString("age"));
                            tenergy.setText(jsonObject.getString("energy"));
                            taveenergy.setText(jsonObject.getString("aveenergy"));
                            tenweight.setText(jsonObject.getString("enweight"));
                            tlenweight.setText(jsonObject.getString("lenweight"));
                            ffm.setText(jsonObject.getString("ffm"));
                            protein.setText(jsonObject.getString("protein"));
                            muscle.setText(jsonObject.getString("muscle"));
                            ucre.setText(jsonObject.getString("ucre"));

                        }
                    } catch (Exception e) {
                        runOnUiThread(() -> Toast.makeText(InformationActivity.this, "信息转化异常", Toast.LENGTH_SHORT).show());
                    }
                    OkHttpClient clientget2 = new OkHttpClient();
                    Request request2 = new Request.Builder()
                            .url(LoginActivity.url+"/weight/getRecentWeight")
                            .addHeader("Cookie", sessionid)
                            .post(RequestBody.create(MediaType.parse("application/json"), json1))
                            .build();
                    Response response2 = clientget2.newCall(request2).execute();
                    String resultget2 = Objects.requireNonNull(response2.body()).string();
                    JSONObject jsonObject2 = new JSONObject(resultget2);
                    tweight2.setText(jsonObject2.getString("weight"));
                    if (!jsonObject.get("username").equals("!@#")) {
                        runOnUiThread(() -> Toast.makeText(InformationActivity.this, "获取成功", Toast.LENGTH_SHORT).show());
                    }
                } catch (Exception e) {
                    runOnUiThread(() -> Toast.makeText(InformationActivity.this, "获取异常", Toast.LENGTH_SHORT).show());
                }
            }).start();
        });

        change.setOnClickListener(v->{
            Intent intent = new Intent(InformationActivity.this,ChangeInformActivity.class);
            startActivity(intent);
        });
    }

}